<?php 
//这个类就是Smarty和框架的桥梁
class SmartyView{
	//保存smarty对象
	private static $smarty = NULL;
	/**
	 * 构造函数，在子类被实例化的时候 比如IndexController被实例化，那么它就会自动执行
	 */
	public function __construct(){
		if(self::$smarty) return;
		//配置Smarty
		//实例化Smarty对象
		$smarty = new Smarty;
		//模版目录
		$smarty->template_dir = 'Application/' . MODULE . '/View/' . CONTROLLER;
		//runtime目录
		$runtimePath = 'Application/Runtime';
		//编译目录
		$compilePath = $runtimePath . '/Compile/' . MODULE . '/' . CONTROLLER . '/';
		is_dir($compilePath) || mkdir($compilePath,0777,true);
		//编译目录设置
		$smarty->compile_dir = $compilePath;
		//缓存目录
		$cachePath = $runtimePath . '/Cache/' . MODULE . '/' . CONTROLLER . '/';
		is_dir($cachePath) || mkdir($cachePath,0777,true);
		//缓存目录设置
		$smarty->cache_dir = $cachePath;
		//开始定界符
		$smarty->left_delimiter = C('LEFT_DELIMITER');
		//结束定界符
		$smarty->right_delimiter = C('RIGHT_DELIMITER');
		//是否缓存
		$smarty->caching = C('CACHE_SWITCH');
		//缓存时间，以秒为单位
		$smarty->cache_lifetime = C('CACHE_LIFETIME');
		//局部不缓存，用nocache标签包裹的部分是不会缓存的,把nocache函数放到了Functions.php
		$smarty->register_block("nocache", "nocache", false);
		//smarty对象保存到静态属性中
		self::$smarty = $smarty;
		
	}
	
	/**
	 * 载入模板
	 */
	protected function display($tpl=NULL){
		if(is_null($tpl)) $tpl = ACTION . '.html';
		self::$smarty->display($tpl,$_SERVER['REQUEST_URI']);
	}
	
	/**
	 * 分配变量
	 */
	protected function assign($var,$value){
		self::$smarty->assign($var,$value);
	}
	
	/**
	 * 判断缓存是否失效
	 */
	protected function is_cached($tpl=NULL){
		if(is_null($tpl)) $tpl = ACTION . '.html';
		return self::$smarty->is_cached($tpl,$_SERVER['REQUEST_URI']);
	}
	
	
}







